package pl.icn.hurtownia.mappings.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import pl.icn.hurtownia.mappings.HurtowniaMapper;

/**
 * Mapper mapujący listy
 *
 */
public abstract class ListMapper {
	
	/**
	 * Metoda mapuje dwie listy
	 * @param mapper
	 * @param sourceObjects
	 * @param destinationClass
	 * @return
	 */
	public static <T, U> List<T> map(HurtowniaMapper mapper, Collection<U> sourceObjects , Class<T> destinationClass) {
		if (mapper == null) {
			throw new NullPointerException("Mapper nie moze byc null");
		}
		if (sourceObjects == null) {
			return null;
		}
		List<T> result = new ArrayList<T>();
		for (U nextItem : sourceObjects) {
			T toAdd = mapper.map(nextItem, destinationClass);
			if (toAdd != null) {
				result.add(toAdd); 
			}
		}
		return result;
	}
	
	/**
	 * Metoda mapuje dwie listy o zadanym Id mappingu
	 * @param mapper
	 * @param sourceObjects
	 * @param destinationClass
	 * @return
	 */
	public static <T, U> List<T> map(HurtowniaMapper mapper, Collection<U> sourceObjects , Class<T> destinationClass, String mapId) {
		if (mapper == null) {
			throw new NullPointerException("Mapper nie moze byc null");
		}
		if (sourceObjects == null) {
			return null;
		}
		List<T> result = new ArrayList<T>();
		for (U nextItem : sourceObjects) {
			T toAdd = mapper.map(nextItem, destinationClass, mapId);
			if (toAdd != null) {
				result.add(toAdd); 
			}
		}
		return result;
	}

}
